package com.mybatis.model;

import com.mybatis.inter.IFreightOperation;

import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.List;

public class FreightDB {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static{
        try{
            reader    = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }

    //根据ID查询记录
    public void getFreight(int id){
        SqlSession session = sqlSessionFactory.openSession();
        Freight freight;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            freight = freightOperation.selectRecordByID(id);
            System.out.println(freight.getDelivery_city());
        } finally {
            session.close();
        }
    }

    //获取各产品的全国货运总量
    public List<LinkedHashMap<String, Object>> getProduct(){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectProduct();
        } finally {
            session.close();
        }
        return listMaps;
    }

    //获取各城市输出货运量
    public List<LinkedHashMap<String, Object>> getDelivery(){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectDelivery();
        } finally {
            session.close();
        }
        return listMaps;
    }

    //获取各城市输入货运量
    public List<LinkedHashMap<String, Object>> getReceipt(){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectReceipt();

        } finally {
            session.close();
        }
        return listMaps;
    }

    //查询各城市输入各种货物数量
    public List<LinkedHashMap<String, Object>> getReceiptProduct(){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectReceiptProduct() ;

        } finally {
            session.close();
        }
        return listMaps;
    }

    //查询各城市输出各种货物数量
    public List<LinkedHashMap<String, Object>> getDeliveryProduct(){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectDeliveryProduct();

        } finally {
            session.close();
        }
        return listMaps;
    }

    //查询某座城市各种货物输入量
    public List<LinkedHashMap<String, Object>> getAReceiptProduct(String city){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectAReceiptProduct(city);

        } finally {
            session.close();
        }
        return listMaps;
    }

    //查询某座城市各种货物输出量
    public List<LinkedHashMap<String, Object>> getADeliveryProduct(String city){
        SqlSession session = sqlSessionFactory.openSession();
        List<LinkedHashMap<String, Object>> listMaps;
        try {
            IFreightOperation freightOperation=session.getMapper(IFreightOperation.class);
            listMaps=freightOperation.selectADeliveryProduct(city);

        } finally {
            session.close();
        }
        return listMaps;
    }

    //打印查询的表
    public void printRecord(List<LinkedHashMap<String, Object>> listMaps){
        for (Map<String, Object> map : listMaps) {
            System.out.println("=============================");
            for (Map.Entry<String, Object> m : map.entrySet()) {
                System.out.print(m.getKey() + "  ");
                System.out.println(m.getValue());
            }
        }

    }

    public static void main(String[] args) {
        FreightDB fdb=new FreightDB();
        //fdb.getFreight(4);
        //fdb.getProduct();
        //fdb.printRecord(fdb.getDelivery());
        fdb.printRecord(fdb.getADeliveryProduct("万州区"));
    }
}
